<?php

use backend\modules\statistics\assets\EchartsAsset;
use yii\bootstrap\BootstrapAsset;
BootstrapAsset::register($this);
EchartsAsset::register($this);

use common\models\business\NewBusinessInspection;
use common\models\business\NewBusinessInspectionData;

//取出距离预出栏报警的天数
$now = time();
if (!Yii::$app->request->get('days')) {
    $outpre = \common\models\sys\SysSetting::getValue('waitout');
} else {
    $outpre = Yii::$app->request->get('days');
}

$outpretime = $now + ($outpre * 24 * 3600);


//出栏预警的体重
$outweight = 130;



$this->title = '仔猪状态分布';



?>


<style>
    .row{
        margin-right: 0;
        margin-left: 0;
    }
</style>


<div class="content margin_top_foot">
    <header class="header fixed_top bg_mx_green">
        <div class="back_text" onclick="window.location.history.go(-1)">
            <div class="arrow_left"></div>
            返回
        </div>
        仔猪状态分布
        <div class="header_icon">
            <span class="iconfont icon-home"></span>
        </div>
    </header>
    <div class="margin_b_10 relative">
        <section class="wrapper site-min-height">
            <!-- page start-->
            <section class="panel">

                <div class="row" style="margin-top:30px;">
                    <div class="col-md-10 col-md-offset-1">
                        <div class="panel-body progress-panel">
                            <form action="" method="get" style="margin-bottom: 21px;width: 80%;">
                                <label for="">输入预出栏天数</label>
                                <input type="text" value="" class="form-control" name="days"
                                       placeholder="距离<?= $outpre ?>天出栏的猪有多少头"/>
                            </form>


                            <div class="task-progress">
                                <h4>仔猪状态分布</h4>
                            </div>
                            <div class="task-option">
                            </div>
                        </div>
                        <table class="table table-hover">
                            <tbody>
                            <tr>
                                <th>
                                    批次
                                </th>
                                <?php foreach (\common\models\business\BusinessPig::$statues as $key => $row) { ?>
                                    <th>
                                        <?= $row ?>
                                    </th>
                                <?php } ?>
                                <th>预出栏</th>
                                <th>
                                    总量
                                </th>
                            </tr>
                            <?php foreach ($result as $key => $row) { ?>
                                <tr>
                                    <td>
                                        <?= $key ?>
                                    </td>
                                    <?php foreach (\common\models\business\BusinessPig::$statues as $k => $v) { ?>
                                        <td>
                                           <?= array_key_exists('num', $row[$k]) ? $row[$k]['num'] : 0 ?>
                                        </td>
                                    <?php } ?>
                                    <td>

                                        <?php
                                        $pouts = (new \yii\db\Query())->select(new \yii\db\Expression('count(*)'))
                                            ->from(['p'=>\common\models\business\BusinessPig::tableName()])
                                            ->where('planout != 0 and planout<=:preout and batch=:batch',[':preout'=>$outpretime,':batch'=>$key])
                                            ->andWhere([
                                                'id'=>(new \yii\db\Query())->select('ins.pigid')
                                                    ->from(['ins'=>NewBusinessInspection::tableName()])
                                                    ->leftJoin(['insd'=>NewBusinessInspectionData::tableName()],'inspectionid=ins.id')
                                                    ->where('insd.fieldname=\'体重\' and convert(insd.val,decimal(10,2))>=:outweight',[':outweight'=>$outweight])
                                                    ->orderBy('ins.createtime desc')
                                                    ->scalar()
                                            ])
                                            ->count();
                                            echo $pouts;
                                        ?>
                                    </td>
                                    <td>
                                        <?= $row['total'] ?>
                                    </td>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-md-10 col-md-offset-1" style="margin-top: 100px;">
                        <div id="pignum" style="height: 350px;width: 90%;"></div>
                    </div>
                </div>
            </section>
    </div>
    <div class="user_navs grid_3 bg_white"></div>
    <!--<footer class="foot_tab fixed_bottom mx_green font_16"></footer>-->
</div>




<script>
    <?php $this->beginBlock('pignum') ?>
    //查看状态数量
    $('.showdetail').click(function(){
        var title = $(this).data('status')+'数量详情';
        $.get('/statistics/pig/get-statusnum',{batch:$(this).data('batch'),status:$(this).data('status'),outtime:<?=$outpretime?>},function(html){
            $('#numdetail').modal();
            $('#numdetail .modal-body').html(html);
            $('#numdetail .modal-title').html(title);
            $("#print").printPreview({
                obj2print:'#printarae',
                width:'810'
            });
        })
    })




    var option = {
        title : {
            text: '仔猪总体状态统计',
            subtext: '',
            x:'center'
        },
        color: ['#3398DB'],
        tooltip : {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis : [
            {
                type : 'category',
                data : <?= json_encode($statusdata['type']) ?>,
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'数量',
                type:'bar',
                barWidth: '60%',
                data:<?= json_encode($statusdata['num']) ?>,
            }
        ]
    };

    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('pignum'));
    myChart.setOption(option);
    <?php $this->endBlock('pignum') ?>
    <?php $this->registerJs($this->blocks['pignum'],\yii\web\View::POS_END) ?>
</script>

